﻿// MoveContinue.cpp: 实现文件
//

#include "pch.h"
#include "LiWi.h"
#include "afxdialogex.h"
#include "MoveContinue.h"
#include "Data.h"
#include "liwishell.h"
#include "MoveContinueData.h"
#include <math.h>

#define M_PI	3.14159265358979323846

//extern CListCtrl m_PointsList;

int num = 0;
// MoveContinue 对话框

IMPLEMENT_DYNAMIC(MoveContinue, CDialogEx)

MoveContinue::MoveContinue(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MoveContinue, pParent)
{

}

MoveContinue::~MoveContinue()
{
}

void MoveContinue::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST1, m_PointsList);
}


BEGIN_MESSAGE_MAP(MoveContinue, CDialogEx)
	ON_BN_CLICKED(IDC_AddPoint, &MoveContinue::OnBnClickedAddpoint)
	ON_BN_CLICKED(IDC_Start, &MoveContinue::OnBnClickedStart)
	ON_BN_CLICKED(IDC_BUTTON2, &MoveContinue::OnBnClickedButton2)
END_MESSAGE_MAP()


// MoveContinue 消息处理程序




BOOL MoveContinue::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	CRect rect;
	m_PointsList.GetClientRect(&rect);
	m_PointsList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);		//整行选择，网格线
	m_PointsList.InsertColumn(0, _T("序号"), LVCFMT_CENTER, rect.Width() / 6, 0);
	m_PointsList.InsertColumn(1, _T("AXIS0"), LVCFMT_LEFT, rect.Width() / 6, 0);
	m_PointsList.InsertColumn(2, _T("AXIS1"), LVCFMT_LEFT, rect.Width() / 6, 0);
	m_PointsList.InsertColumn(3, _T("AXIS2"), LVCFMT_LEFT, rect.Width() / 6, 0);
	m_PointsList.InsertColumn(4, _T("AXIS3"), LVCFMT_LEFT, rect.Width() / 6, 0);
	m_PointsList.InsertColumn(5, _T("速度"), LVCFMT_LEFT, rect.Width() / 6, 0);
	((CButton*)GetDlgItem(IDC_Mode0))->SetCheck(true);
	GetDlgItem(IDC_LimitSp)->SetWindowTextW(_T("30"));
	GetDlgItem(IDC_StartAngle)->SetWindowTextW(_T("10"));
	GetDlgItem(IDC_StopAngle)->SetWindowTextW(_T("45"));
	GetDlgItem(IDC_LimitRad)->SetWindowTextW(_T("50"));
	GetDlgItem(IDC_DaojiaoRad)->SetWindowTextW(_T("10"));

	GetDlgItem(IDC_Speed_3)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Accel_3)->SetWindowTextW(_T("1000"));
	GetDlgItem(IDC_Decel_3)->SetWindowTextW(_T("1000"));

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}




//新增点位
void MoveContinue::OnBnClickedAddpoint()
{
	// TODO: 在此添加控件通知处理程序代码
	MoveContinueData ChildWin;
	ChildWin.DoModal();
	CString str;
	//str.Format(_T("%d"), num);
	str.Format(_T("%d"), m_PointsList.GetItemCount());
	int nIndex = m_PointsList.InsertItem(m_PointsList.GetItemCount(), str);
	m_PointsList.SetItemText(nIndex, 1, ChildWin.point0);
	m_PointsList.SetItemText(nIndex, 2, ChildWin.point1);
	m_PointsList.SetItemText(nIndex, 3, ChildWin.point2);
	m_PointsList.SetItemText(nIndex, 4, ChildWin.point3);
	m_PointsList.SetItemText(nIndex, 5, ChildWin.speed);
	num++;
}

//删除
void MoveContinue::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码
	int nIndex = m_PointsList.GetItemCount();
	m_PointsList.DeleteItem(nIndex - 1);
}


//启动
void MoveContinue::OnBnClickedStart()
{
	// TODO: 在此添加控件通知处理程序代码
	CString speed, accel, decel
		, limitSp		//限制速度
		, startAngle	//起始减速角度
		, stopAngle		//停止减速角度
		, limitRad		//小圆限速半径
		, daojiaoRad;	//倒角半径
	int changeSp		//变速
		, Merge			//连续插补
		, isDev			//自动拐角速度
		, isLimit		//自动小圆限速
		, isDaojiao;	//自动倒角
	GetDlgItem(IDC_Speed_3)->GetWindowTextW(speed);
	GetDlgItem(IDC_Accel_3)->GetWindowTextW(accel);
	GetDlgItem(IDC_Decel_3)->GetWindowTextW(decel);
	GetDlgItem(IDC_LimitSp)->GetWindowTextW(limitSp);
	GetDlgItem(IDC_StartAngle)->GetWindowTextW(startAngle);
	GetDlgItem(IDC_StopAngle)->GetWindowTextW(stopAngle);
	GetDlgItem(IDC_LimitRad)->GetWindowTextW(limitRad);
	GetDlgItem(IDC_DaojiaoRad)->GetWindowTextW(daojiaoRad);

	changeSp = ((CButton*)GetDlgItem(IDC_ChangeSp))->GetCheck() ? 1 : 0;
	Merge = ((CButton*)GetDlgItem(IDC_Merge))->GetCheck() ? 1 : 0;
	isDev = ((CButton*)GetDlgItem(IDC_isDev))->GetCheck() ? 1 : 0;
	isLimit = ((CButton*)GetDlgItem(IDC_isLimit))->GetCheck() ? 1 : 0;
	isDaojiao = ((CButton*)GetDlgItem(IDC_isDaojiao))->GetCheck() ? 1 : 0;

	//设置轴参数
	PMC_Base(Data::handle, 4, Data::axisList);
	for (size_t i = 0; i < 4; i++)
	{
		int enable = 0;
		PMC_GetAxisEnable(Data::handle, i, &enable);
		if (enable == 0)
		{
			MessageBox(TEXT("请检查轴0-3使能情况"));
			return;
		}
		PMC_SetAtype(Data::handle, i, 1);
		PMC_SetUnits(Data::handle, i, 100);
		PMC_SetSpeed(Data::handle, i, _ttof(speed));
		PMC_SetAccel(Data::handle, i, _ttof(accel));
		PMC_SetDecel(Data::handle, i, _ttof(decel));
		PMC_SetMerge(Data::handle, i, Merge);
		PMC_SetSpeedDyna(Data::handle, i, changeSp);
	}

	//设置减速类型
	int cmode = 0;
	if (((CButton*)GetDlgItem(IDC_isDev))->GetCheck())		//自动拐角减速
	{
		cmode += 2;
		PMC_SetDecelAngle(Data::handle, 0, (float)(_ttof(startAngle) * M_PI / 180.0));
		PMC_SetStopAngle(Data::handle, 0, (float)(_ttof(stopAngle) * M_PI / 180.0));
	}
	if (((CButton*)GetDlgItem(IDC_isLimit))->GetCheck())	//自动小圆限速
	{
		cmode += 8;
		PMC_SetFullSpRadius(Data::handle, 0, _ttof(limitRad));
	}
	if (((CButton*)GetDlgItem(IDC_isDaojiao))->GetCheck())
	{
		cmode += 32;
		PMC_SetFullSpRadius(Data::handle, 0, _ttof(daojiaoRad));
	}
	PMC_SetCornerMode(Data::handle, 0, cmode);
	PMC_SetForceSpeed(Data::handle, 0, _ttof(limitSp));

	//获取点位值
	CString pointAxis0, pointAxis1, pointAxis2, pointAxis3, pointSpeed;
	for (size_t i = 0; i < m_PointsList.GetItemCount(); i++)
	{
		pointAxis0 = m_PointsList.GetItemText(i, 1);
		pointAxis1 = m_PointsList.GetItemText(i, 2);
		pointAxis2 = m_PointsList.GetItemText(i, 3);
		pointAxis3 = m_PointsList.GetItemText(i, 4);
		pointSpeed = m_PointsList.GetItemText(i, 5);
		float pointAxisList[4] = { _ttof(pointAxis0),_ttof(pointAxis1), _ttof(pointAxis2), _ttof(pointAxis3) };
		PMC_MovePara(Data::handle, 0, (char*)"SPEED", 0, _ttof(pointSpeed));
		if (((CButton*)GetDlgItem(IDC_Mode0))->GetCheck())		//绝对
		{
			PMC_MoveAbsSp(Data::handle, 4, Data::axisList, pointAxisList);
		}
		else													//相对
		{
			PMC_MoveSp(Data::handle, 4, Data::axisList, pointAxisList);
		}

	}

}

